//package com.example.demo;
//
//import com.mongodb.BasicDBObject;
//import org.junit.jupiter.api.Test;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.data.mongodb.core.aggregation.Aggregation;
//import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
//import org.springframework.data.mongodb.core.aggregation.LookupOperation;
//import org.springframework.data.mongodb.core.aggregation.ProjectionOperation;
//import org.springframework.data.mongodb.core.query.Criteria;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.web.bind.annotation.GetMapping;
//import reactor.core.publisher.Flux;
//import reactor.core.publisher.Mono;
//
//import java.io.IOException;
//import java.io.Serializable;
//import java.util.Arrays;
//import java.util.List;
//
//import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
//
//public class ResponseInfo<T> implements Serializable {
//    private String msg;
//    private Integer code;
//    private Integer status;
//    Object data;
//
//    public static <T> ResponseInfo<T> ok (T monoBody) {
//        final ResponseInfo<T> responseInfo = new ResponseInfo<>();
//        responseInfo.setCode(0);
//        responseInfo.setData(monoBody);
//        responseInfo.setMsg("ok");
//        return responseInfo;
//    }
//
//    // 省略其他 get/set 和 error 等
//
//
//    public String getMsg() {
//        return msg;
//    }
//
//    public void setMsg(String msg) {
//        this.msg = msg;
//    }
//
//    public Integer getCode() {
//        return code;
//    }
//
//    public void setCode(Integer code) {
//        this.code = code;
//    }
//
//    public Object getData() {
//        return data;
//    }
//
//    public void setData(Object data) {
//        this.data = data;
//    }
//
//    public Integer getStatus() {
//        return status;
//    }
//
//    public void setStatus(Integer status) {
//        this.status = status;
//    }
//
//    @Test
//    void test3() {
//        final Flux<Integer> oddSource = Flux.fromIterable(Arrays.asList(1, 3, 5, 7, 9));
//        final Flux<Integer> evenSource = Flux.fromIterable(Arrays.asList(2, 4, 6, 8, 10));
//        final Mono<Integer> monoSource = Mono.just(11);
////        final List<Integer> totalList = Flux.concat(oddSource,evenSource, monoSource)
////                .collectList().block();
//
//        System.out.println(oddSource.metrics().collectList().block());
////        Flux<Integer> integerFlux = monoSource.mergeWith(Mono.just(100));
////        System.out.println(integerFlux.collectList().block());
//    }
//
//    @GetMapping("/download")
//    public Mono<Void> downloadByWriteWith(ServerHttpResponse response) throws IOException {
//        response.getHeaders().set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=demo.xls");
//        response.getHeaders().add("Accept-Ranges", "bytes");
//        Flux<DataBuffer> flux = export(1000);
//        return response.writeWith(flux);
//    }
//
////    public Flux<DataBuffer> export(int dataRow){
////        return Flux.create(sink -> {
////            sink.next(stringBuffer(ExcelUtil.startWorkbook()));
////            sink.next(stringBuffer(ExcelUtil.startSheet()));
////            sink.next(stringBuffer(ExcelUtil.startTable()));
////
////            //write title row
////            sink.next(stringBuffer(ExcelUtil.writeTitleRow(Sets.newHashSet("title","content"))));
////            //write data row
////            IntStream.rangeClosed(1,dataRow).forEach(i -> {
////                try {
////                    TimeUnit.MILLISECONDS.sleep(100);
////                } catch (InterruptedException e) {
////                    e.printStackTrace();
////                }
////                sink.next(stringBuffer(ExcelUtil.writeDataRow(Lists.newArrayList("title"+i,"content"+i))));
////            });
////
////            sink.next(stringBuffer(ExcelUtil.endTable()));
////            sink.next(stringBuffer(ExcelUtil.endSheet()));
////            sink.next(stringBuffer(ExcelUtil.endWorkbook()));
////
////            sink.complete();
////        });
////    }
//
////    LookupOperation lookupOperation = LookupOperation.newLookup().
////            from("posts").
////            localField("userid").
////            foreignField("authors").
////            as("post");
////
////    AggregationOperation match = Aggregation.match(Criteria.where("post").size(1));
////
////
////    Aggregation aggregation = Aggregation.newAggregation(lookupOperation, match);
////
////    List<BasicDBObject> results = mongoOperation.aggregate(aggregation, "users", BasicDBObject.class).getM
//
//
//
//import java.util.Date;
//import java.util.List;
//import java.util.Map;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.mongodb.core.MongoTemplate;
//import org.springframework.data.mongodb.core.aggregation.Aggregation;
//import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
//import org.springframework.data.mongodb.core.aggregation.LookupOperation;
//import org.springframework.data.mongodb.core.aggregation.ProjectionOperation;
//import org.springframework.data.mongodb.core.query.Criteria;
//import org.springframework.stereotype.Service;
//
//import com.hollysys.tn.common.base.service.impl.BaseServiceImpl;
//import com.hollysys.tn.entity.MaterialUsage;
//
//import lombok.Data;
//
//    @Data
//    class ResultEntity{
//        private Double amount;
//        private String _id;
//        private String name;
//    }
//
//
//    @Service
//    public class MaterialUsageService extends BaseServiceImpl<MaterialUsage>{
//        @Autowired
//        private MongoTemplate mongoTemplate;
//
//        public List<MaterialUsage> query(Date beginDate, Date endDate) {
//
//            LookupOperation lookupToLots = LookupOperation.newLookup().
//                    from("lots").//关联表名 lots
//                    localField("lotId").//关联字段
//                    foreignField("_id").//主表关联字段对应的次表字段
//                    as("entity");//查询结果集合名
//            LookupOperation lookupToMaterials = LookupOperation.newLookup().
//                    from("materials").//关联表名 materials
//                    localField("materialId").//关联字段
//                    foreignField("_id").//主表关联字段对应的次表字段
//                    as("bojo");//查询结果集合名
//            //Criteria criteriaToLots = new Criteria().and("entity.beginAt").lte(endDate).and("entity.endAt").gte(beginDate);
//            Criteria criteriaToLots = new Criteria().and("entity.fillingRate").gte(0);
//            AggregationOperation matchToLots = Aggregation.match(criteriaToLots);
//
//            ProjectionOperation project  = Aggregation.project("entity._id","amount","bojo.name");
//            project.andInclude("_id");
//            Aggregation counts = Aggregation.newAggregation(lookupToLots,matchToLots,lookupToMaterials,project);
//
//            List<Map> results =  mongoTemplate.aggregate(counts,"materialusage",Map.class).getMappedResults();
//            System.out.println(results.size());
//
//            //List<ResultEntity> results =  mongoTemplate.aggregate(counts,"materialusage",ResultEntity.class).getMappedResults();
//
//            System.out.println(results.get(0).getName());
//            return null;
//        }
//    }
//
//
//
//}